package com.bj58.groupbuy.action.controllers;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.web.bind.ServletRequestUtils;
import org.wuba.WAQ;

import com.bj58.groupbuy.action.model.BelongLocal;
import com.bj58.groupbuy.action.model.ChinahrIndustry;
import com.bj58.groupbuy.action.model.ChinahrJob;
import com.bj58.groupbuy.action.model.ChinahrSource;
import com.bj58.groupbuy.action.model.DisplayLocal;
import com.bj58.groupbuy.action.model.IRSubMailVo;
import com.bj58.groupbuy.action.model.InfoSourcePlatform;
import com.bj58.groupbuy.action.model.JxedtFlowSource;
import com.bj58.groupbuy.action.model.NetFlowSource;
import com.bj58.groupbuy.action.model.NewBelongCate;
import com.bj58.groupbuy.action.model.NewDisplayCate;
import com.bj58.groupbuy.action.model.User;
import com.bj58.groupbuy.action.model.UserSubMailVo;
import com.bj58.groupbuy.action.service.BelongLocalService;
import com.bj58.groupbuy.action.service.ChinahrIndustryService;
import com.bj58.groupbuy.action.service.ChinahrJobService;
import com.bj58.groupbuy.action.service.ChinahrSourceService;
import com.bj58.groupbuy.action.service.DisplayLocalService;
import com.bj58.groupbuy.action.service.IRReportService;
import com.bj58.groupbuy.action.service.IRSubMailService;
import com.bj58.groupbuy.action.service.InfoSourcePlatformService;
import com.bj58.groupbuy.action.service.IntReportMainService;
import com.bj58.groupbuy.action.service.JxedtFlowSourceService;
import com.bj58.groupbuy.action.service.NetFlowSourceService;
import com.bj58.groupbuy.action.service.NewBelongCateService;
import com.bj58.groupbuy.action.service.NewDisplayCateService;
import com.bj58.groupbuy.action.service.UserSubMailService;
import com.bj58.groupbuy.action.util.MyServletRequestUtils;
import com.bj58.groupbuy.action.util.SomePublicUtil;
import com.bj58.groupbuy.service.locator.ServiceLocator;
import com.bj58.groupbuy.util.IOUtils;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.BeatContext;
import com.bj58.wf.mvc.MvcController;
import com.bj58.wf.mvc.actionresult.ContentResult;
import com.bj58.wf.mvc.annotation.GET;
import com.bj58.wf.mvc.annotation.POST;
import com.bj58.wf.mvc.annotation.Path;


/**自定义报表 - 工具、级联逻辑等
 * @author huangliang
 *
 */
@Path("irtools")
public class IntReportToolsController extends MvcController{
	private DisplayLocalService displayLocalService = ServiceLocator.get(DisplayLocalService.class);
	private BelongLocalService belongLocalService = ServiceLocator.get(BelongLocalService.class);
	private SomePublicUtil spu = new SomePublicUtil();
	public static Hashtable<Integer, IRSubMailVo> subEmailTable = new  Hashtable<Integer, IRSubMailVo>(); //报表邮件订阅 - 查询条件缓存  k=userid+reportid  
	public static Hashtable<Integer, UserSubMailVo> userEmailTable = new  Hashtable<Integer, UserSubMailVo>(); //报表邮件订阅 - 查询条件缓存  k=userid+reportid  
	private IRSubMailService iRSubMailService = ServiceLocator.get(IRSubMailService.class);
	private UserSubMailService userSubMailService = ServiceLocator.get(UserSubMailService.class);
	private IRReportService iRReportService = ServiceLocator.get(IRReportService.class);
	private IntReportMainService IntReportMainService = ServiceLocator.get(IntReportMainService.class);
	private NewDisplayCateService newDisplayCateService = ServiceLocator.get(NewDisplayCateService.class);
	private NewBelongCateService newBelongCateService = ServiceLocator.get(NewBelongCateService.class);
	private InfoSourcePlatformService infoSourcePlatformService = ServiceLocator.get(InfoSourcePlatformService.class);
	private NetFlowSourceService netFlowSourceService = ServiceLocator.get(NetFlowSourceService.class);
	private JxedtFlowSourceService jxedtFlowSourceService = ServiceLocator.get(JxedtFlowSourceService.class);
	private ChinahrSourceService chinahrSourceService = ServiceLocator.get(ChinahrSourceService.class);
	private ChinahrIndustryService chinahrIndustryService = ServiceLocator.get(ChinahrIndustryService.class);
	private ChinahrJobService chinahrJobService = ServiceLocator.get(ChinahrJobService.class);
	
	
	/**展现城市 - 级联查询
	 * @return
	 */
	@Path("displaylocal")
	@POST	
	public ActionResult searchDisplayLocal() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				String city1 = MyServletRequestUtils.getStringParameter(request, "city1","");
				String city2 = MyServletRequestUtils.getStringParameter(request, "city2","");
				try {
					JSONObject json = new JSONObject();
					List<DisplayLocal> disCityListTwo =  new ArrayList<DisplayLocal>();//2级列表
					List<DisplayLocal> disCityListThree =  new ArrayList<DisplayLocal>();//3级列表
					String result = "right";
					if(!"".equals(city1) && "".equals(city2)){  //1、2级联
						disCityListTwo = displayLocalService.getLevelTwo(city1);//2级
					}else if(!"".equals(city1) && !"".equals(city2)){//1、2、3级联
						disCityListThree = displayLocalService.getLevelThree(city1, city2);
					}else{
						result = "error";
					}
					json.accumulate("disCityListTwo", disCityListTwo);
					json.accumulate("disCityListThree", disCityListThree);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}
	
	/**归属城市 - 级联查询
	 * @return
	 */
	@Path("belongcity")
	@POST
	public ActionResult searchBelongCity() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				String city1 = MyServletRequestUtils.getStringParameter(request, "city1","");
				String city2 = MyServletRequestUtils.getStringParameter(request, "city2","");
				try {
					JSONObject json = new JSONObject();
					List<BelongLocal> belongCityListTwo =  new ArrayList<BelongLocal>();//2级列表
					List<BelongLocal> belongCityListThree =  new ArrayList<BelongLocal>();//3级列表
					String result = "right";
					if(!"".equals(city1) && "".equals(city2)){  //1、2级联
						belongCityListTwo = belongLocalService.getLevelTwo(city1);//2级
					}else if(!"".equals(city1) && !"".equals(city2)){//1、2、3级联
						belongCityListThree = belongLocalService.getLevelThree(city1, city2);
					}else{
						result = "error";
					}
					json.accumulate("belongCityListTwo", belongCityListTwo);
					json.accumulate("belongCityListThree", belongCityListThree);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}
	
	
	/**报表邮件订阅 - 添加订阅信息入库
	 * @return
	 */
	@Path("reportSubMail")
	@GET
	@POST
	public ActionResult reportSubMail() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				
				String sendEmail = org.springframework.web.bind.ServletRequestUtils.getStringParameter(request, "emailAds","");
				String sendFre = org.springframework.web.bind.ServletRequestUtils.getStringParameter(request, "freValue","");
				String subName = org.springframework.web.bind.ServletRequestUtils.getStringParameter(request, "subName","");
				int reportId = org.springframework.web.bind.ServletRequestUtils.getIntParameter(request, "reportId", 0);
				String result = "error";
				JSONObject json = new JSONObject();
				try {
					int userId = (int)spu.getUserByCookie(request).getId();
					//后端邮箱校验
					boolean checkEmail = true;
					for(String ad : sendEmail.split("/")){
						ad = ad.trim();
						if(!(ad.endsWith("@58.com")||ad.endsWith("@58ganji.com")||ad.endsWith("@zhiyou.ganji.com")||ad.endsWith("@ganji.com")||ad.endsWith("@doumi.com"))){
							checkEmail = false;
						}
					}
					
					if(reportId > 0 && userId > 0 && checkEmail){
						IRSubMailVo vo = subEmailTable.get(userId + reportId);
						vo.setSendFre(sendFre);
						vo.setSendEmail(sendEmail);
						vo.setSubName(subName);
						vo.setState(1);
						vo.setReportName(iRReportService.getById(reportId).getReportName());
						
						int saveId =  (Integer)iRSubMailService.save(vo);
						if(saveId > 0 ){
							result = "success";
						}
					}
				
				} catch (Exception e) {
					e.printStackTrace();
				}finally{
					try {
						json.accumulate("result", result);
						response.setContentType("text/plain; charset=utf-8");
						PrintWriter pw = response.getWriter();
						pw.write(json.toString());
						response.flushBuffer();
						IOUtils.closeQuietly(pw);
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
			}
		};
	}
	
	
	/**报表邮件下载 - 添加订阅信息入库
	 * @return
	 */
	@Path("userSubMail")
	@GET
	@POST
	public ActionResult userSubMail() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				
				String subName = ServletRequestUtils.getStringParameter(request, "DownsubName", "");// 别名
				int reportId = org.springframework.web.bind.ServletRequestUtils
						.getIntParameter(request, "reportId", 0);
				String result = "error";
				JSONObject json = new JSONObject();
				try {
					int userId = (int) spu.getUserByCookie(request).getId();
					String sendEmail = spu.getUserByCookie(request)
							.getUsername() + "@58ganji.com";
					if (reportId > 0 && userId > 0) {
						UserSubMailVo vo = userEmailTable
								.get(userId + reportId);
						vo.setSendEmail(sendEmail);
						vo.setSubName(subName);
						vo.setState(1);
						vo.setReportName(iRReportService.getById(reportId)
								.getReportName());

						int saveId = (Integer) userSubMailService.save(vo);
						if (saveId > 0) {
							result = "success";

						}
					}

				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						json.accumulate("result", result);
						response.setContentType("text/plain; charset=utf-8");
						PrintWriter pw = response.getWriter();
						pw.write(json.toString());
						response.flushBuffer();
						IOUtils.closeQuietly(pw);
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
			}
		};
	}
	
	/**访问记录
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("deprecation")
	@Path("/visitrecord")
	@GET
	@POST
	public ActionResult visitRecord() throws Exception {
		try{
			HttpServletRequest request = beat.getRequest();
			String url=java.net.URLDecoder.decode(MyServletRequestUtils.getStringParameter(request, "url",""));
			if(url.contains("_")){ //去掉小版本号
				url = url.substring(0,url.indexOf("_"));
			}
			User user = spu.getUserByCookie(request);
			String userName = user.getUsername();
			IntReportMainService.inserVisitRecord(userName, url);
//			log.info(url);
		}catch(Exception e){
			System.out.print(e.toString());
		}
		
		return new ContentResult("1");
	
		
	}
	
	/**查询记录
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("finally")
	@Path("/searchrecord")
	@GET
	@POST
	public ActionResult searchRecord() throws Exception {
		try{
			
			HttpServletRequest request = beat.getRequest();
			String pathId = MyServletRequestUtils.getStringParameter(request, "pathId","");
			String groupBy = MyServletRequestUtils.getStringParameter(request, "groupBy","");
			String searchCols = MyServletRequestUtils.getStringParameter(request, "searchCols","");
			String templateType = MyServletRequestUtils.getStringParameter(request, "templateType","");//查询指标显示类型  1： 默认显示全部字段
			if("1".equals(templateType)){
				searchCols = "default all";
			}
			
			String menuId = pathId.substring(0,pathId.indexOf("_"));
			
			User user = spu.getUserByCookie(request);
			String userName = user.getUsername();
			
			
			log.info("search record :" + menuId + " || " + groupBy + " || " + userName +" || " + searchCols );
			IntReportMainService.inserSearchRecord(menuId, groupBy ,userName,searchCols);
		}catch(Exception e){
			log.error("",e);
		}finally{
			return new ContentResult("1");
		}
		
	
		
	}
	
	/**展现类别 - 级联查询 （新）
	 * @return
	 */
	@Path("newdiscate")
	@POST
	public ActionResult searchNewDisplayCate() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				String business_type_name = MyServletRequestUtils.getStringParameter(request, "business_type_name","");
				business_type_name = WAQ.forSQL().escapeSql(business_type_name );

				String cate1 = MyServletRequestUtils.getStringParameter(request, "cate1","");
				
				String cate2 = MyServletRequestUtils.getStringParameter(request, "cate2","");
				
				String isBizValue = MyServletRequestUtils.getStringParameter(request, "isBiz","false");
				
				boolean isBiz = false;
				if("true".equals(isBizValue)){
					isBiz = true;
				}
				try {
					JSONObject json = new JSONObject();
					List<NewDisplayCate> disCateListOne =  new ArrayList<NewDisplayCate>();//1级列表
					List<NewDisplayCate> disCateListTwo =  new ArrayList<NewDisplayCate>();//2级列表
					List<NewDisplayCate> disCateListThree =  new ArrayList<NewDisplayCate>();//3级列表
					String result = "right";
					if(!"".equals(business_type_name) && !"null".equals(business_type_name) && "".equals(cate1)){  //频道 、 1级 级联
						disCateListOne = newDisplayCateService.getLevelOne(business_type_name , isBiz);
					}else if(!"".equals(cate1) && "".equals(cate2)){  //1、2级联
						disCateListTwo = newDisplayCateService.getLevelTwo(business_type_name,cate1, isBiz);//2级
					}else if(!"".equals(cate1) && !"".equals(cate2)){//1、2、3级联
						disCateListThree = newDisplayCateService.getLevelThree(business_type_name,cate1, cate2, isBiz);
					}else{
						result = "error";
					}
					json.accumulate("disCateListOne", disCateListOne);
					json.accumulate("disCateListTwo", disCateListTwo);
					json.accumulate("disCateListThree", disCateListThree);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}
	
	
	/**归属类别 - 级联查询（新）
	 * @return
	 */
	@Path("newbelongcate")
	@POST
	public ActionResult searchNewBelongCate() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				String business_type_name = MyServletRequestUtils.getStringParameter(request, "business_type_name","");
				String cate1 = MyServletRequestUtils.getStringParameter(request, "cate1","");
				String cate2 = MyServletRequestUtils.getStringParameter(request, "cate2","");
				String isBizValue = MyServletRequestUtils.getStringParameter(request, "isBiz","0");
				
				boolean isBiz = false;
				if("true".equals(isBizValue)){
					isBiz = true;
				}
				try {
					JSONObject json = new JSONObject();
					List<NewBelongCate> belongCateListOne =  new ArrayList<NewBelongCate>();//1级列表
					List<NewBelongCate> belongCateListTwo =  new ArrayList<NewBelongCate>();//2级列表
					List<NewBelongCate> belongCateListOneThree =  new ArrayList<NewBelongCate>();//3级列表
					String result = "right";
					if(!"".equals(business_type_name) && !"null".equals(business_type_name) && "".equals(cate1)){  //频道 、 1级 级联
						belongCateListOne = newBelongCateService.getLevelOne(business_type_name , isBiz);
					}else if(!"".equals(cate1) && "".equals(cate2)){  //1、2级联
						belongCateListTwo = newBelongCateService.getLevelTwo(business_type_name,cate1 , isBiz);//2级
					}else if(!"".equals(cate1) && !"".equals(cate2)){//1、2、3级联
						belongCateListOneThree = newBelongCateService.getLevelThree(business_type_name,cate1, cate2 , isBiz);
					}else{
						result = "error";
					}
					json.accumulate("belongCateListOne", belongCateListOne);
					json.accumulate("belongCateListTwo", belongCateListTwo);
					json.accumulate("belongCateListThree", belongCateListOneThree);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}
	
	
	/**信息source级联
	 * @return
	 */
	@Path("infosourceSearch")
	@POST
	public ActionResult sourceSearch() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				String platform = MyServletRequestUtils.getStringParameter(request, "infoPlatform","");
				try {
					JSONObject json = new JSONObject();
					List<InfoSourcePlatform> infoSourceList =  new ArrayList<InfoSourcePlatform>();
					String result = "right";
					if(!"".equals(platform)){ 
						infoSourceList = infoSourcePlatformService.getSourceList(platform);
					}else{
						result = "error";
					}
					json.accumulate("infoSourceList", infoSourceList);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}
	
	
	/**流量source级联
	 * @return
	 */
	@Path("flowsourcesearch")
	@POST
	public ActionResult flowSourceSearch() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
//				String platform = ServletRequestUtils.getStringParameter(request, "platform","");
				String source1 = MyServletRequestUtils.getStringParameter(request, "source1","");
				String source2 = MyServletRequestUtils.getStringParameter(request, "source2","");
				String source3 = MyServletRequestUtils.getStringParameter(request, "source3","");
				try {
					JSONObject json = new JSONObject();
					List<NetFlowSource> flowSourceListTwo =  new ArrayList<NetFlowSource>();//2级列表
					List<NetFlowSource> flowSourceListThree =  new ArrayList<NetFlowSource>();//3级列表
					List<NetFlowSource> flowSourceListFour =  new ArrayList<NetFlowSource>();//4级列表
					
					String result = "right";
					if(!"".equals(source1) && "".equals(source2)  && "".equals(source3) ){  //1-2级联
						flowSourceListTwo = netFlowSourceService.getSource2List(source1);
					}else if (!"".equals(source1) && !"".equals(source2) && "".equals(source3)){  //2-3级联
						flowSourceListThree = netFlowSourceService.getSource3List(source1,source2);
					}else if (!"".equals(source1) && !"".equals(source2) && !"".equals(source3)){  //3-4级联
						flowSourceListFour = netFlowSourceService.getSource4List(source1,source2,source3);
					}
					
					else{
						result = "error";
					}
					json.accumulate("flowSourceListTwo", flowSourceListTwo);
					json.accumulate("flowSourceListThree", flowSourceListThree);
					json.accumulate("flowSourceListFour", flowSourceListFour);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}
	
	
	
	/**驾校一点通流量source级联
	 * @return
	 */
	@Path("jxedtSourceSearch")
	@POST
	public ActionResult jxedtSourceSearch() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				String source1 = MyServletRequestUtils.getStringParameter(request, "source1","");
				String pathId = MyServletRequestUtils.getStringParameter(request, "pathId","");
				String platform = "";
				//根据pathId来判定是PC还是M的报表
				if(pathId.contains("359")){
					platform = "PC";
				}else if(pathId.contains("360")){
					platform = "M";
				}	
				try {
					JSONObject json = new JSONObject();
					List<JxedtFlowSource> jxedtSourceTwoList =  new ArrayList<JxedtFlowSource>();
					String result = "right";
					
					if(!"".equals(source1)){ 
						jxedtSourceTwoList = jxedtFlowSourceService.getSource2List(platform,source1);
					}else{
						result = "error";
					}
					json.accumulate("jxedtSourceTwoList", jxedtSourceTwoList);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}
	
	/**
	 *取得文档类别二列表
	 * @param option
	 * @return
	 */
	@Path("getCate2")
	@POST
	@GET
	public ActionResult getCate2(){
		return new ActionResult() {
			@Override
			public void render(BeatContext beat) {
				try {
					HttpServletRequest request = beat.getRequest();
					HttpServletResponse response = beat.getResponse();
					String Cate1 = ServletRequestUtils.getStringParameter(
							request, "Cate1", "");
					String channelname = ServletRequestUtils.getStringParameter(
							request, "ChannelName", "");
					List<String> cate2List = new ArrayList<String>();
					List<NewDisplayCate> disCateListTwo =  new ArrayList<NewDisplayCate>();
					cate2List.add("全部");
					if("".equals(Cate1))
					{
						disCateListTwo = newDisplayCateService.getLevelOne(channelname, false);//2级
						for (int i = 0; i < disCateListTwo.size(); i++) {
							cate2List.add(disCateListTwo.get(i).getCate1_name());
						}
					}
					else {
						disCateListTwo = newDisplayCateService.getLevelTwo(channelname,Cate1, false);//2级
						for (int i = 0; i < disCateListTwo.size(); i++) {
							cate2List.add(disCateListTwo.get(i).getCate2_name());
						}
					}
					
					
					
					StringBuilder sb = new StringBuilder("[");
					for (int i = 0; i < cate2List.size(); i++) {
						String cate2 = cate2List.get(i);
						sb.append("{'value':'" + cate2 + "'}");
						if ((i + 1) < cate2List.size()) {
							sb.append(",");
						}
					}
					sb.append("]");
					JSONArray array = JSONArray.fromObject(sb.toString());
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(array.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}
	
	
	
	
	/**中华英才流量source级联
	 * @return
	 */
	@Path("chrflowsourcesearch")
	@POST
	public ActionResult chrFlowSourceSearch() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				String source1 = MyServletRequestUtils.getStringParameter(request, "source1","");
				String source2 = MyServletRequestUtils.getStringParameter(request, "source2","");
				String source3 = MyServletRequestUtils.getStringParameter(request, "source3","");
				
				String pathId = MyServletRequestUtils.getStringParameter(request, "pathId","");
				String platform = "";
				//根据pathId来判定是PC还是M的报表
				if(pathId.contains("382")){
					platform = "PC";
				}else if(pathId.contains("397")){
					platform = "M";
				}	
				try {
					JSONObject json = new JSONObject();
					List<ChinahrSource> chrSourceTwoList =  new ArrayList<ChinahrSource>();//2级列表
					List<ChinahrSource> chrSourceThreeList =  new ArrayList<ChinahrSource>();//3级列表
					List<ChinahrSource> chrSourceFourList =  new ArrayList<ChinahrSource>();//4级列表
					
					String result = "right";
					if(!"".equals(source1) && "".equals(source2)  && "".equals(source3) ){  //1-2级联
						chrSourceTwoList = chinahrSourceService.getSource2(platform,source1);
					}else if (!"".equals(source1) && !"".equals(source2) && "".equals(source3)){  //2-3级联
						chrSourceThreeList = chinahrSourceService.getSource3(platform,source1,source2);
					}else if (!"".equals(source1) && !"".equals(source2) && !"".equals(source3)){  //3-4级联
						chrSourceFourList = chinahrSourceService.getSource4(platform,source1,source2,source3);
					}
					
					else{
						result = "error";
					}
					json.accumulate("chrSourceTwoList", chrSourceTwoList);
					json.accumulate("chrSourceThreeList", chrSourceThreeList);
					json.accumulate("chrSourceFourList", chrSourceFourList);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}

	

	/**中华英才-行业级联
	 * @return
	 */
	@Path("chrindustrysearch")
	@POST
	public ActionResult chrIndustrySearch() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				String cate1_name = MyServletRequestUtils.getStringParameter(request, "cate1_name","");
				try {
					JSONObject json = new JSONObject();
					List<ChinahrIndustry> chrIndustryTwoList =  new ArrayList<ChinahrIndustry>();
					String result = "right";
					if(!"".equals(cate1_name)){ 
						chrIndustryTwoList = chinahrIndustryService.getAllCate2(cate1_name);
					}else{
						result = "error";
					}
					json.accumulate("chrIndustryTwoList", chrIndustryTwoList);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}

	
	

	/**中华英才职位级联
	 * @return
	 */
	@Path("chrjobsearch")
	@POST
	public ActionResult chrjobsearch() {
		return new ActionResult() {
			@Override
			public void render(BeatContext beat)  {
				HttpServletRequest request = beat.getRequest();
				HttpServletResponse response = beat.getResponse();		
				String cate1_name = MyServletRequestUtils.getStringParameter(request, "cate1_name","");
				String cate2_name = MyServletRequestUtils.getStringParameter(request, "cate2_name","");
				
				try {
					JSONObject json = new JSONObject();
					List<ChinahrJob> chrJobTwoList =  new ArrayList<ChinahrJob>();//2级列表
					List<ChinahrJob> chrJobThreeList =  new ArrayList<ChinahrJob>();//3级列表
					
					String result = "right";
					if(!"".equals(cate1_name) && "".equals(cate2_name)  ){  //1-2级联
						chrJobTwoList = chinahrJobService.getCate2(cate1_name);
					}else if (!"".equals(cate1_name) && !"".equals(cate2_name)){  //2-3级联
						chrJobThreeList = chinahrJobService.getCate3(cate1_name,cate2_name);
					}
					
					else{
						result = "error";
					}
					json.accumulate("chrJobTwoList", chrJobTwoList);
					json.accumulate("chrJobThreeList", chrJobThreeList);
					json.accumulate("result", result);
					response.setContentType("text/plain; charset=utf-8");
					PrintWriter pw = response.getWriter();
					pw.write(json.toString());
					response.flushBuffer();
					IOUtils.closeQuietly(pw);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
	}

	
}
